package robotour.gui.map;

import robotour.gui.map.gps.MapView;
import robotour.util.Setup;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.JPanel;
import robotour.navi.gps.input.GPXIO;
import robotour.navi.gps.Track;
import robotour.navi.gps.TrackPoint;

/**
 *
 * @author  PC
 */
class TrackManagerPanel extends JPanel {

    /** Creates new form TrackManagerPanel */
    public TrackManagerPanel() {
        initComponents();

        File dir = new File(Setup.getRoot() + "tracks/");
        System.out.println("tracks root = " + dir);
        File[] list = dir.listFiles();

        System.out.println("Loading tracks:");

        for (File file : list) {
            try {
                add(file);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }


//        add(DeviceManager.getGps().getActiveLog());

    }
    MapViewPanel mapPanel = new MapViewPanel(new MapView());
    List<Track> tracks = new ArrayList<Track>();
    Track selectedTrack;

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        pointsList = new javax.swing.JList();
        javax.swing.JPanel panel1 = mapPanel;
        jLabel1 = new javax.swing.JLabel();
        tracksList = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();

        pointsList.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        pointsList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                pointsListMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(pointsList);

        panel1.setLayout(null);

        jLabel1.setText("trackpoints");

        tracksList.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tracksListActionPerformed(evt);
            }
        });

        jButton1.setText("open");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(panel1, javax.swing.GroupLayout.DEFAULT_SIZE, 222, Short.MAX_VALUE)
                    .addComponent(tracksList, 0, 222, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 176, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 176, Short.MAX_VALUE)
                    .addComponent(jButton1))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tracksList, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(panel1, javax.swing.GroupLayout.DEFAULT_SIZE, 253, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 233, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // open track file and add it to list
        this.add(GPXIO.showLoadTrackDialog(this));
    }//GEN-LAST:event_jButton1ActionPerformed

    private void pointsListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_pointsListMouseClicked
        if (evt.getClickCount() == 2) {
            int index = pointsList.locationToIndex(evt.getPoint());
//             System.out.println("Double clicked on Item " + index);

            TrackPoint trkpt = (TrackPoint) pointsList.getSelectedValue();

// TODO            DeviceManager.getBody().putOn(trkpt);

//            mapPanel.map.setEye(trkpt.getPoint());
        }
    }//GEN-LAST:event_pointsListMouseClicked

    private void tracksListActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tracksListActionPerformed
        selectedTrack = (Track) tracksList.getSelectedItem();
        if (selectedTrack != null) {
// TODO            DeviceManager.getBody().setTrack(selectedTrack);
//            mapPanel.map.zoomTo(selectedTrack);
        }
        repaint();
    }//GEN-LAST:event_tracksListActionPerformed
    Map<Track, File> filemap = new HashMap<Track, File>();

    public void add(Track track) {
        tracks.add(track);
    }

    /**
     *  adds Track loaded from specified gpx file
     * @param trackfile
     */
    public void add(File trackfile) {
        Track track = GPXIO.loadTrack(trackfile);
        add(track);
        filemap.put(track, trackfile);

    }

    @Override
    public void repaint() {
        if (tracksList != null) {
            tracksList.removeAllItems();
            for (Track trk : tracks) {
                tracksList.addItem(trk);
            }
            tracksList.setSelectedItem(selectedTrack);
        }
        if (pointsList != null && selectedTrack != null) {

            pointsList.setListData(selectedTrack.track().toArray());

        }
        super.repaint();
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    javax.swing.JList pointsList;
    javax.swing.JComboBox tracksList;
    // End of variables declaration//GEN-END:variables
}
